//
//  CTSubjectDetailViewController.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/6/14.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTSubjectDetailViewController.h"
#import "CTTabBarController.h"
#import "CTPickImageViewController.h"
#import "CTLoginViewController.h"
#import "CTUserViewController.h"

//head
#import "CTHeader.h"

//view
#import "CTSubjectDetailView.h"

//model
#import "CTShareItem.h"
#import "CTHomePageItems.h"

//function
#import "CTDebugManager.h"
#import "CTLogin.h"
#import "CTDataGenerator.h"
#import "CTShare.h"
#import "CTShareView.h"
#import "CTRemindView.h"
#import "CTAppLanguage.h"
#import "CTLogic.h"
#import "CTGenRequestData.h"
#import "CTRequestUtil.h"
#import "MobClick.h"

@interface CTSubjectDetailViewController ()<CTSubjectDetailViewDelegate,CTShareViewDelegate>
{
    CTSubjectDetailView * subjectDetailView;
    
    CTShareView * shareView;
    
    CTSubjectItem * subjectItem;
    
    NSString * shareUrlStr;
    UIImage * currentShareImg;
    CTLogic * logic;
}
@end

@implementation CTSubjectDetailViewController
@synthesize hotSpotsItem;

- (void)dealloc
{
    [subjectDetailView removeFromSuperview];
    subjectDetailView = nil;
    
    hotSpotsItem = nil;
    currentShareImg = nil;
    
    [shareView removeFromSuperview];
    shareView = nil;
    
    shareUrlStr = nil;
    subjectItem = nil;
    
    logic = nil;
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:CT_NOTI_PUBLISH_SUCCESS object:nil];
    
    [CTDebugManager showDebugLogInfo:@"dealloc : CTSubjectDetailViewController Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        logic = [[CTLogic alloc] init];
        
        //注册发布内容通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(publishSuccess:) name:CT_NOTI_PUBLISH_SUCCESS object:nil];
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    //统计
    [MobClick beginLogPageView:@"CTSubjectDetailViewController"];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    //统计
    [MobClick endLogPageView:@"CTSubjectDetailViewController"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    subjectItem = [CTDataGenerator generateSubjectItems:hotSpotsItem];
    subjectDetailView = [[CTSubjectDetailView alloc] initWithFrame:self.view.bounds withItem:subjectItem];
    subjectDetailView.delegate  = self;
    [self.view addSubview:subjectDetailView];
    
    
    [CTRequestUtil requestImgWithUrl:subjectItem.url withCompletionSuccessHandler:^(UIImage *img) {
        dispatch_async(dispatch_get_main_queue(), ^{
            currentShareImg = img;
        });
    } withFailedHandler:^(NSError *error) {
        
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - subject view delegate -

- (void)backBtnAction:(CTSubjectDetailView *) view
{
    CTTabBarController * tabbarVC = (CTTabBarController *)self.navigationController.tabBarController;
    [tabbarVC showTabbarWithAnimate:YES];
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)shareBtnAction:(CTSubjectDetailView *) view withShareUrl:(NSString *) shareUrl
{
    if ([CTLogin isUserLoginState] == YES) {
        //已登录
        shareUrlStr = shareUrl;
        
        //share view
        shareView = [[CTShareView alloc] init];
        shareView.delegate = self;
        [shareView showShareViewOn:self.view.window withShareViewType:CTShareViewTypeDefault];
        
    }else{
        //未登录
        CTLoginViewController * loginVC = [[CTLoginViewController alloc] init];
        UINavigationController * loginNav = [[UINavigationController alloc] initWithRootViewController:loginVC];
        loginNav.navigationBarHidden = YES;
        
        [self presentViewController:loginNav animated:YES completion:^{
            
        }];
    }
}
- (void)attentionBtnAction:(CTSubjectDetailView *)view withItem:(CTSubjectItem *)item withBool:(BOOL)attenBool
{
    
    if ([CTLogin isUserLoginState] == YES) {
        //已登录
        
        if (attenBool == YES) {
            [logic followPersonRequest:[CTGenRequestData genRequestFollowPseronItem:subjectItem.user.uid] successCompletionHandler:^{
                dispatch_async(dispatch_get_main_queue(), ^{
                    [CTDebugManager showDebugLogInfo:@"关注成功！"];
                    [[NSNotificationCenter defaultCenter] postNotificationName:CT_NOTI_FOLLOWING_SUCCESS object:nil];
                });
            } failedHandler:^{
                dispatch_async(dispatch_get_main_queue(), ^{
                    [CTDebugManager showErrorLogInfo:@"关注失败！"];
                });
            }];
        }else{
            [logic cancelFollowPersonRequest:[CTGenRequestData genRequestCancelFollowPseronItem:subjectItem.user.uid] successCompletionHandler:^{
                dispatch_async(dispatch_get_main_queue(), ^{
                    [CTDebugManager showDebugLogInfo:@"取消关注成功！"];
                    [[NSNotificationCenter defaultCenter] postNotificationName:CT_NOTI_CANCEL_FOLLOWING_SUCCESS object:nil];
                });
            } failedHandler:^{
                dispatch_async(dispatch_get_main_queue(), ^{
                    [CTDebugManager showErrorLogInfo:@"取消关注失败！"];
                });
            }];
        }
        
    }else{
        //未登录
        CTLoginViewController * loginVC = [[CTLoginViewController alloc] init];
        UINavigationController * loginNav = [[UINavigationController alloc] initWithRootViewController:loginVC];
        loginNav.navigationBarHidden = YES;
        
        [self presentViewController:loginNav animated:YES completion:^{
            
        }];
    }
}
- (void)cameraBtnAction:(CTSubjectDetailView *) view
{
    if ([CTLogin isUserLoginState] == YES) {
        //已登录
        CTPickImageViewController * pickImageVC = [[CTPickImageViewController alloc] init];
        UINavigationController * pickImageNav = [[UINavigationController alloc] initWithRootViewController:pickImageVC];
        pickImageNav.navigationBarHidden = YES;
        [self presentViewController:pickImageNav animated:YES completion:^{
            
        }];
        
    }else{
        //未登录
        CTLoginViewController * loginVC = [[CTLoginViewController alloc] init];
        UINavigationController * loginNav = [[UINavigationController alloc] initWithRootViewController:loginVC];
        loginNav.navigationBarHidden = YES;
        
        [self presentViewController:loginNav animated:YES completion:^{
            
        }];
    }
}

- (void)headerBtnAction:(CTSubjectDetailView *) view withItem:(CTSubjectItem *) item
{
    if ([CTLogin isUserLoginState] == YES) {
        //已登录
        
        CTTabBarController * tabbarVC = (CTTabBarController *)self.navigationController.tabBarController;
        [tabbarVC hideTabbarWithAnimate:YES];
        CTUserViewController * userVC = [[CTUserViewController alloc] init];
        userVC.pushType = CTUserViewControllerPushFromTypeOther;
        userVC.levelType = CTUserViewControllerLevelOther;
        userVC.userUid = item.user.uid;
        [self.navigationController pushViewController:userVC animated:YES];
        
        
    }else{
        //未登录
        CTLoginViewController * loginVC = [[CTLoginViewController alloc] init];
        UINavigationController * loginNav = [[UINavigationController alloc] initWithRootViewController:loginVC];
        loginNav.navigationBarHidden = YES;
        
        [self presentViewController:loginNav animated:YES completion:^{
            
        }];
    }
}

#pragma mark - share view delegate -

//试图移除
-(void)viewDidRemoved:(CTShareView *)view
{
    shareView = nil;
    shareView.delegate = nil;
}
//分享到微信朋友圈
- (void)shareViewShareToWechatFriends:(CTShareView *) view withImage:(UIImage *)image
{
    
    CTShareItem * shareItem = [[CTShareItem alloc] init];
    shareItem.content = subjectItem.share_url;
    shareItem.defaultContent = subjectItem.share_url;
    shareItem.img = [UIImage imageNamed:@"60x3"];
    shareItem.descr = [CTAppLanguage localizedStringForKey:@"share_topic_describe" withValue:nil withTable:@"Language"];;
    shareItem.title = [CTAppLanguage localizedStringForKey:@"share_title" withValue:nil withTable:@"Language"];
    shareItem.url = subjectItem.share_url;
    
    
    [CTShare shareItem:shareItem shareChannel:CTShareChannelWechatFriend publishType:CTPublishTypeNews shareSuccessHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_successed" withValue:nil withTable:@"Language"]];
        });
    } shareFailedHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_failed" withValue:nil withTable:@"Language"]];
        });
    }];
}
//分享到QQ空间
- (void)shareViewShareToQQSpace:(CTShareView *)view withImage:(UIImage *)image
{
    CTShareItem * shareItem = [[CTShareItem alloc] init];
    shareItem.content = subjectItem.share_url;
    shareItem.defaultContent = subjectItem.share_url;
    shareItem.img = [UIImage imageNamed:@"60x3"];
    shareItem.descr = [CTAppLanguage localizedStringForKey:@"share_topic_describe" withValue:nil withTable:@"Language"];;
    shareItem.title = [CTAppLanguage localizedStringForKey:@"share_title" withValue:nil withTable:@"Language"];
    shareItem.url = subjectItem.share_url;
    
    
    [CTShare shareItem:shareItem shareChannel:CTShareChannelQQSpace publishType:CTPublishTypeImage shareSuccessHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_successed" withValue:nil withTable:@"Language"]];
        });
    } shareFailedHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_failed" withValue:nil withTable:@"Language"]];
        });
    }];
}
//分享到新浪微博
- (void)shareViewShareToSina:(CTShareView *)view withImage:(UIImage *)image
{
    CTShareItem * shareItem = [[CTShareItem alloc] init];
    shareItem.content = subjectItem.share_url;
    shareItem.defaultContent = subjectItem.share_url;
    shareItem.img = [UIImage imageNamed:@"60x3"];
    shareItem.descr = [CTAppLanguage localizedStringForKey:@"share_topic_describe" withValue:nil withTable:@"Language"];;
    shareItem.title = [CTAppLanguage localizedStringForKey:@"share_title" withValue:nil withTable:@"Language"];
    shareItem.url = subjectItem.share_url;
    
    [CTShare sinaBtnAction:shareItem shareSuccessHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_successed" withValue:nil withTable:@"Language"]];
        });
    } shareFailedHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_failed" withValue:nil withTable:@"Language"]];
        });
    }];

}
//分享到微信
- (void)shareViewShareToWeChat:(CTShareView *)view withImage:(UIImage *)image
{
    CTShareItem * shareItem = [[CTShareItem alloc] init];
    shareItem.defaultContent = subjectItem.describe;
    shareItem.descr = subjectItem.describe;
    shareItem.url = subjectItem.share_url;
    shareItem.img = [UIImage imageNamed:@"60x3"];
    shareItem.content = [CTAppLanguage localizedStringForKey:@"share_topic_describe" withValue:nil withTable:@"Language"];;
    shareItem.title = [CTAppLanguage localizedStringForKey:@"share_title" withValue:nil withTable:@"Language"];
    
    [CTShare shareItem:shareItem shareChannel:CTShareChannelWechat publishType:CTPublishTypeNews shareSuccessHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_successed" withValue:nil withTable:@"Language"]];
        });
    } shareFailedHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_failed" withValue:nil withTable:@"Language"]];
        });
    }];
}
//分享到QQ
- (void)shareViewShareToQQ:(CTShareView *)view withImage:(UIImage *)image
{
    
    CTShareItem * shareItem = [[CTShareItem alloc] init];
    shareItem.defaultContent = subjectItem.describe;
    shareItem.descr = subjectItem.describe;
    shareItem.url = subjectItem.share_url;
    shareItem.img = [UIImage imageNamed:@"60x3"];
    shareItem.content = [CTAppLanguage localizedStringForKey:@"share_topic_describe" withValue:nil withTable:@"Language"];;
    shareItem.title = [CTAppLanguage localizedStringForKey:@"share_title" withValue:nil withTable:@"Language"];
    [CTShare shareItem:shareItem shareChannel:CTShareChannelQQ publishType:CTPublishTypeNews shareSuccessHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_successed" withValue:nil withTable:@"Language"]];
        });
    } shareFailedHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_failed" withValue:nil withTable:@"Language"]];
        });
    }];
}



#pragma mark - notification -


-(void)publishSuccess:(id)sender
{
    CTTabBarController * tabbarVC = (CTTabBarController *)self.navigationController.tabBarController;
    [tabbarVC showTabbarWithAnimate:YES];
    [self.navigationController popToRootViewControllerAnimated:YES];
}


@end
